//
//  SLWelocomeViewController.swift
//  0319iOSweiboDemo
//
//  Created by Tang on 16/3/22.
//  Copyright © 2016年 大天朝. All rights reserved.
//

import UIKit
import SDWebImage

class SLWelocomeViewController: UIViewController {

    //MARK: - 属性
    //头像底部约束
    var iconViewBottomCons: NSLayoutConstraint?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        perpareUI()
        
        //在发送网络请求之前.就先使用本地图片.有就用本地的.没有就使用默认的(网络之前之后各设置一次)
        if let urlString = SLUserAccountViewModel.sharedInstance.userAccount?.avatar_large {
            let url = NSURL(string: urlString)
            self.iconView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar_default_big"))
        }
        //获取用户的头像信息.显示头像.需要等网络请求完了才加载.
        //即使图片在本地有缓存也需要等到网络请求完才设置
        SLUserAccountViewModel.sharedInstance.loadUserInfo { (error) -> () in
            if error != nil {
                SLPrint(msg: "请求出错: \(errno)")
                return
            }
            //没有出错.给头像赋值
//            SLPrint(msg: "没有出错")
            if let urlString = SLUserAccountViewModel.sharedInstance.userAccount?.avatar_large {
                let url = NSURL(string: urlString)
                self.iconView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar_default_big"))
            }
            
        }
    }
    
    private func perpareUI() {
        //添加子控件
        view.addSubview(bkgImageView)
        view.addSubview(iconView)
        view.addSubview(welcomeLabel)
        
        //添加约束
        //背景
        bkgImageView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(self.view)
        }
        
        //头像X 
        iconView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.view)
            make.bottom.equalTo(self.view).offset(-160)
            make.size.equalTo(CGSize(width: 85, height: 85))
        }
        
        //欢迎label
        //X
        welcomeLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView)
            make.top.equalTo(iconView.snp_bottom).offset(16)
        }
        
    }
    
    //界面已经显示了
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        moveAnimated()
    }
    
    //头像移动的动画
    private func moveAnimated() {
        
        //动画
        //usingSpringWithDamping 弹簧效果
        UIView.animateWithDuration(1, delay: 0.1, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
//            self.iconViewBottomCons?.constant = -(UIScreen.mainScreen().bounds.height - 160)
            //snp_updateConstraints 更新约束
            self.iconView.snp_updateConstraints(closure: { (make) -> Void in
                //更新约束不能修改参照.只能改变常量
                make.bottom.equalTo(self.view).offset(-(UIScreen.mainScreen().bounds.height - 160))
            })
             self.view.layoutIfNeeded()
            }) { (_) -> Void in
              UIView.animateWithDuration(SLDefaultDuration, animations: { () -> Void in
                self.welcomeLabel.alpha = 1
                }, completion: { (_) -> Void in
//                    print("动画完成")
//                    let appDelegate = UIApplication.sharedApplication().delegate as? AppDelegate
//                    appDelegate?.switchRootViewController(SLMainViewController())
                    AppDelegate.outSwitchRootViewController(SLMainViewController())
              })
            
        }

    }
    //MARK: - 懒加载
    //背景
    private lazy var bkgImageView: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    
    private lazy var iconView: UIImageView = {
        let imageView = UIImageView(image: UIImage(named: "avatar_default_big"))
      //设置圆角
        imageView.layer.cornerRadius = 42.5
        imageView.layer.masksToBounds = true//超出裁剪区域不显示
        
        imageView.backgroundColor = UIColor.blackColor()
        return imageView
    }()
    
    private lazy var welcomeLabel: UILabel = {
        let label = UILabel()
        label.text = "欢迎归来"
        label.font = UIFont.systemFontOfSize(15)
        label.sizeToFit()
        return label
    }()

}
